package m4_routting;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.64.140");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");

        Channel channel = connectionFactory.newConnection().createChannel();

        //定义交换机
        channel.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);

        //向交换机发送消息
        while (true){
            System.out.println("输入消息:");
            String msg = new Scanner(System.in).nextLine();
            System.out.println("输入路由键:");
            String key = new Scanner(System.in).nextLine();
            channel.basicPublish(
                    "direct_logs",         //向哪一个交换机发送消息
                    key,                   //路由键关键词
                    null,
                    msg.getBytes());
        }
    }
}
